package com.first.dao.impl;

import com.first.dao.StatisticsDao;
import com.first.util.DBUtil;
import com.first.util.Echarts;
import com.first.util.EchartsDate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class StatisticsDaoImpl implements StatisticsDao {
    /**
     * 销量统计
     *
     * @return
     */
    @Override
    public List<Echarts> querySales(Echarts echarts,String start,String end) {

        String sql="select b.bookname,sum(o_price) from o_order_wj w,book b,o_order o where o.o_orderid=w.o_orderid and o.status=1 and  b.bookid=w.bookid and  o_orderoutdate BETWEEN ? and ?  GROUP BY bookname ORDER BY o_price desc  LIMIT 0,10";
        List<List> list = DBUtil.queryList(sql,start,end);
        List<Echarts> echarts1 = echarts.setEcharts(list);
        return echarts1;
    }

    /**
     * 查看出图书的销售额和采购额
     *
     * @param echarts
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<Echarts> queryProfit(Echarts echarts, String start, String end) {


        ArrayList<Echarts> echartss = new ArrayList<>();
        Echarts echarts1 = new Echarts();
        Echarts echarts2 = new Echarts();
        String sql1 = "select sum(o_price) from o_order_wj oow,o_order oo where oow.o_orderid=oo.o_orderid and oo.status=1 and  oo.o_orderoutdate BETWEEN ? and ?";
        List<List> lists1 = DBUtil.queryList(sql1, start, end);

        String sql2 = "select sum(t_price) from b_order_wj bow,b_order bo WHERE bo.orderstatus=4 and bow.orderid=bo.orderid and bo.storagedate BETWEEN ? and ?";
        List<List> lists2 = DBUtil.queryList(sql2, start, end);
        if (lists1.get(0).get(0)!=null ){
            echarts1.setName("销售额");
            echarts1.setValue((Double) lists1.get(0).get(0));
            echartss.add(echarts1);
        }
        if (lists2.get(0).get(0)!=null){
            echarts2.setName("采购额");
            echarts2.setValue((Double) lists2.get(0).get(0));
            echartss.add(echarts2);
        }
        return echartss;
    }

    /**
     * 查看库存
     *
     * @return
     */
    @Override
    public List<EchartsDate> getStock(EchartsDate echarts) {

        String sql="select t.type ,sum(count) from b_type t,book b where t.typeid=b.typeid GROUP BY t.typeid";
        List<List> lists = DBUtil.queryList(sql);
        EchartsDate echartsDate = new EchartsDate();
        List<EchartsDate> echarts1 = echartsDate.setEcharts(lists);
        return echarts1;
    }


}
